﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Itop.GpsCar.Model;

namespace Itop.GpsCar.Web
{
    public partial class PlayBack : System.Web.UI.Page
    {
        public string deviceID = "1";
        public bool DrawLine = false;
        public bool RunMark = false;
        public DateTime timeStamp;
        public string toTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        public string fromTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        public int selectIndex = 5;
        public class MapPoint
        {
            public MapPoint(double wd, double jd)
            {
                x = wd;
                y = jd;
            }
            public double x;
            public double y;
            public string pl = "0";
            public double speed = 0;
            public string strTime = "00:00:00";
        }
        public static IList<MapPoint> marlist = new List<MapPoint>();
        public static IList<MapPoint> pointlist = new List<MapPoint>();
        public static IList<long> posID = new List<long>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {  
                DateTime dtemp = System.DateTime.Now;        
                toTime = dtemp.ToString("yyyy-MM-dd HH:mm:ss");
                fromTime = dtemp.AddHours(-1).ToString("yyyy-MM-dd HH:mm:ss");
                fromDate.Value = fromTime;
                toDate.Value = toTime;
                freq.SelectedIndex = selectIndex;
            }
           
        }      
        public void PLAY_Click(object sender, EventArgs e)
        {
            fromTime = fromDate.Value;
            toTime = toDate.Value;
            selectIndex = freq.SelectedIndex;
            if ((sender as HtmlInputButton).ID == "PLAY")
            {
                PLAY.Style.Value = "display:none";              
                STOP.Style.Value = "display:block";
                marlist.Clear();
                pointlist.Clear();
                posID.Clear();
                object obj = Global.SqlMapper.GetObject("SelectdevicecarrierList", " where device_id = " + deviceID);
                System.Collections.Generic.IList<position> posList = Global.SqlMapper.GetList<position>("SelectpositionList", " where device_id = " + deviceID + " order by timestamp asc");
                DrawLine = true;
                foreach (position pos in posList)
                {
                    MapPoint mp = new MapPoint(pos.latitude, pos.longitude);
                    mp.speed = pos.speed;
                    mp.strTime = pos.timestamp.ToString();
                    if (obj != null)
                    {
                        object car = Global.SqlMapper.GetObject("SelectcarrierList", " where idCarrier = " + (obj as devicecarrier).carrier_id);
                        if (car != null)
                        {
                            mp.pl = (car as carrier).plate;
                        }
                        else
                        {
                            mp.pl = "0";
                        }
                    }
                    else
                    {
                        mp.pl = "0";
                    }
                    if (!posID.Contains(pos.idPosition))
                    {
                        pointlist.Add(mp);
                        posID.Add(pos.idPosition);
                    }
                }
                RunMark = true;
            } 
        }
    }
}
